package com.million.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.eduservice.entity.EduTeacher;
import com.million.eduservice.entity.vo.teacher.TeacherListVo;
import com.million.eduservice.entity.vo.teacher.TeacherQuery;
import com.million.eduservice.mapper.EduTeacherMapper;
import com.million.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.million.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-03-19
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery (Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //按照sort降序排序
        queryWrapper.orderByDesc("gmt_create");
        //判断teacherQuery是否为空，也就是前端有无传过来参数,没有，就直接分页查询
        if (teacherQuery == null) {
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //开始时间大于等于  这里的column是数据库的字段名
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //结束时间小于等于
            queryWrapper.le("gmt_create", end);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void updateTeacherById (TeacherListVo teacherListVo) {
        EduTeacher eduTeacher = new EduTeacher();
        BeanUtils.copyProperties(teacherListVo, eduTeacher);
        int update = baseMapper.updateById(eduTeacher);
        if (update == 0) {
            throw new GuliException(20001, "更新失败");
        }
    }

    /******************************前台****************************************/
    //根据id降序，查询四个讲师
    //写入到缓存中
    @Cacheable(value = "teacher",key="'selectFourTeacher'")
    @Override
    public List<EduTeacher> selectFourTeacher () {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").last("limit 4");
        List<EduTeacher> eduTeacherList = baseMapper.selectList(wrapper);
        return eduTeacherList;
    }

    //前端页面分页查询讲师列表
    @Override
    public Map<String, Object> getTeacherFrontPage (Page<EduTeacher> teacherPage) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        baseMapper.selectPage(teacherPage, queryWrapper);
        List<EduTeacher> records = teacherPage.getRecords();
        long current = teacherPage.getCurrent();
        long pages = teacherPage.getPages();
        long size = teacherPage.getSize();
        long total = teacherPage.getTotal();
        boolean hasNext = teacherPage.hasNext();
        boolean hasPrevious = teacherPage.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("records", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
}
